<?php
require_once('./mysqlUtil.php');

//获取请求参数
$username=$_POST['userName'];
$password=$_POST['ps'];

//预处理及绑定
$sql = "select * from t_user where username=? and password=?";
$stmt=$conn->prepare($sql);

//设置参数并执行
$stmt->bind_param("ss",$username,$password); //第一个参数ss 代表后面每个参数类型， 如果$gender是int类型，则应写成si

$data = array('state' => true, 'msg' => '登录成功');
if(preg_match('/^[a-zA-Z]\w{3,17}$/',$username) && preg_match('/^[a-zA-Z][a-zA-Z0-9_]{2,15}$/',$password) ){
    $stmt->execute();
    $result=$stmt->get_result();
    if ($result->num_rows > 0) {
        //存入session
        session_start();//开启session
        $_SESSION['username']=$username;
        //记录登录时间
        $time=date('Y/m/d H:i:s');
        //$sql="UPDATE t_user SET time=".$time ."WHERE (username=".$username.");";
        $sql="UPDATE php_shop_project.t_user SET time='$time' WHERE `username`='$username';";
        $conn->query($sql);
        // 输出数据
        $data['msg']=$sql;  
        echo json_encode($data);
    } else {
        $data['state']=false;
        $data['msg']='密码或用户名错误';  
        echo json_encode($data);   
    }
    
}else {
    $data['state']=false;
    $data['msg']='密码或用户名错误';  
    echo json_encode($data);   
}


 
$conn->close();
?>